
** NOTE TO USERS: EDIT LOOPS USING THE GLOBAL $nbatch IN THE MASTER FILE ACCORDING TO COMPUTING RESOURCES AVAIALBLE.
** RESULTS REPORTED IN MANUSCRIPT USED 2000 LOOPS

** IF YOU EXPERIENCE ERRORS IN RUNNING THIS CODE, MAKE SURE YOU HAVE THE REQUIRED R LIBRARIES INSTALLED (SEE exploration_sampling.R file for details)

********************************************************************************
*** Create Randomisation Inference data 
********************************************************************************
	
	global loops = $nbatch   // $nbatch defined in "00 Master File.do"
	set seed 120993
	set sortseed 120993 
	
	timer clear
	
	clear 
	gen batch = .
	save "$rianalysis/ri_data_stratarand.dta", replace
		
	qui forval loop = 1/$loops {
	    
			timer on 1
		
			*** Load clean data	
			use "$covidclean/smscovid_clean.dta", clear
			
			*** Keep relevant observations: consent taken and above 18 years of age, non-missing treatment arms
			keep if consent==1 & age>=18
			keep if ~missing(treatment_arm)
			
			*** Keep relevant variables
			keep stratum pairnumber treatment_pooled treatment_arm roundofinterview scrambledid act_sd act_hw behavior behavior_sd behavior_hw
			
			*** Create a newround variable for round of interviews where mistake shares are coded up as one round and then code subsequent rounds accordingly
			gen newround = roundofinterview
			recode newround (1=1) (2=1) (3=1) (4=1) (5=1) //use mistake rounds as prior rounds
			recode newround (6=2) (7=3) (8=4) (9=5) (10=6) (11=7) (12=8) (13=9) (14=10)		
			
			*** Replace treatment_arm as missing for after Round 5 i.e. newround>1 (after the mistake in the sampling was fixed)
			gen treatment = treatment_arm 
			replace treatment = . if newround > 1
			la var treatment "Re-assigned control and treatment arms"			
			
			*** This chunk of code randomly reassigns arms in the mistake period to those who were treated
			forval behavior = 1/2 {
			    gen rand = runiform() 
				sort rand, stable
				gen orig_treat = _n if _n <=10
				drop rand
				gen rand = runiform() if !missing(orig_treat)
				sort rand, stable
				gen new_treat = _n if _n <=10
				forval i = 1/10 {
					summ new_treat if orig_treat == `i'
					replace treatment = `r(mean)' if treatment_arm == `i' & newround==1 & behavior == `behavior'
				}
				drop rand orig_treat new_treat 
			}
			bys newround behavior stratum: gen n = _n 
			preserve 
				keep newround behavior stratum treatment n 
				tempfile treatments 
				save `treatments', replace
			restore 
			bys newround behavior stratum: gen rand = runiform()
			sort newround behavior stratum rand, stable 
			bys newround behavior stratum: replace n = _n 
			drop treatment 
			merge 1:1 newround behavior stratum n using `treatments'
			drop _merge	rand		

			*** Randomly assign treatment status, holding shares constant within round 
			bys newround behavior stratum: egen origtreatnum = sum(treatment_pooled)
			gen rand = runiform()
			sort newround behavior stratum rand, stable
			by newround behavior stratum: replace treatment = 0 if _n > origtreatnum & newround != 1
			assert treatment == . | treatment == 0 if newround > 1
			
			drop treatment_pooled rand
			gen treatment_pooled = treatment != 0 
			keep scrambledid treatment newround act_hw act_sd behavior_* pairnumber stratum 
			gen batch = `loop'
			tempfile priortreatments
			save `priortreatments', replace 
			
			*** Run adaptive sampling codes, from round 2 to 10
			forval round = 2/10 {
				foreach b in sd hw {	
				
					noi di as text "Running Loop - `loop' for Round - `round' and behavior - `b' at time: `c(current_time)'"
					
					use `priortreatments', clear
					*** For each round, after the mistake rounds (Round 5), first call social distancing outcome and then hand washing outcome
					*** Use mistake rounds as priors for round 2
					preserve 
						rename act_`b' outcome
						drop if treatment==0 | missing(treatment) | missing(outcome)
						keep if newround < `round' & behavior_`b'==1
						cap export delimited using "$rianalysis/reassignedpriors_round.csv", nolabel replace
						while _rc {     
							sleep 1000
							capture export delimited using "$rianalysis/reassignedpriors_round.csv", nolabel replace
						}
					restore
					
					*** Run adaptive (exploration sampling) R code
					// NOTE: If the rsource function does not work properly, try using the alternative "rscript" command below:
					*rscript using "$dofiles/exploration_sampling.R"
					rsource using "$dofiles/exploration_sampling.R", rpath(`"$Rterm_path"') roptions("--vanilla")  
					
					*** Create adaptive shares for reassignment 
					import delimited using "$rianalysis/adaptivetreatment_round.csv", clear
					gen n = 1
					collapse (sum) n, by(treatment)
					egen total = total(n)
					gen rate = n/total
					keep treatment rate
					
					*** Generate the adaptive shares as local for reassignment to the mastertreatments data
					levelsof treatment, local(treatments)
					local ratelist = ""
					foreach arm of local treatments {
						summ rate if treatment == `arm'
						local ratelist = "`ratelist'" + " `r(mean)'"
					}
					di "`ratelist'" 

					*** Keep only data for the current round and behavior
						use `priortreatments', clear
						keep if newround==`round' & behavior_`b'==1 
						sort stratum pairnumber, stable
						by stratum pairnumber: gen n = _n
						keep if treatment!=0
						gen rand = runiform()
						sort rand, stable
						sort stratum pairnumber, stable 					
						by stratum pairnumber: replace n = _n
						stratarand if n == 1, gentreat(adapttreat) strata(stratum) group_percentages(`ratelist') replace exact
						sort stratum pairnumber adapttreat
						by stratum pairnumber: replace adapttreat = adapttreat[_n-1] if !missing(adapttreat[_n-1])
						replace adapttreat = runiformint(1,10) if missing(adapttreat)
						drop rand
						tempfile adapt_treated
						keep scrambledid adapttreat 
						assert !missing(adapttreat)
						count if !missing(adapttreat)
						save `adapt_treated', replace
					
					*** Merge the data with `priortreatments' file to update the allocations for this round and behavior
						use `priortreatments', clear
						merge 1:1 scrambledid using `adapt_treated'
						drop _merge
						replace treatment = adapttreat if !missing(adapttreat)
						drop adapttreat 
						save `priortreatments', replace 
									
				}
				
				local round = `round' + 1
					
			}
			
			*** Save each iterative round data 
			noi di "Merging Loop - `loop' data to master RI file at time: `c(current_time)'"
		
			preserve 
				use "$rianalysis/ri_data_stratarand.dta", clear
				append using `priortreatments'
				replace batch = `loop' if missing(batch)
				la val treatment treatment
				save "$rianalysis/ri_data_stratarand.dta", replace 
			restore
			
			
			timer off 1
			noi di as text "======================================================================"
			noi di as text "=== Time for this loop (#`loop') and average time per loop so far ==="
			noi timer list 1
			noi di as text "======================================================================"
			    
	}
